//DM File for Assorted Procedures called by some HTML-related code
//Jon88 also puts other miscellanious garbage here from time to time :)

//****************************
//Overrided Procedures Section
//Nothing to see here

//The Topic() procs. They get called whenever we need to pass information from HTML to DM
//client/Topic(href,href_list[],hsrc)
//	..()

//Note: This will likely never be used. Who knows, though?
//world/Topic(T,Addr,Master,Key)
//	..()



//****************************
//Brand new Procedures Section

proc
	StartsWith(href as text, lookingfor as text) //A useful proc used to see if something is at the start of some text
		var/temp = copytext(href, 1, lentext(lookingfor)+1)
		if(ckey(temp) == ckey(lookingfor))
			return 1
		else
			return 0

	FindPage(pagename as text) //Finds, and returns, a Page datum
		var/Page/P = new()
		P = P.GetPage(pagename)
		return P


//LoW's amazing proc!
//This code will eventually be added to the normal CotW stuff.
//Currently the world/Topic proc is not being used, but a modified version for the client will be.

/*
#define validation_key null

topic_action {proc/action()}

world.Topic(href,addr,master,key)
	if(!master) if(key != validation_key) return null
	//handle simple cases
	switch(href) {if("comm-mode") return world.version;if("ping") return 1;if("cpu") return world.cpu}
	//handle other cases
	var {list/href_list = params2list(href);topic_action/action;action_name;argument}
	if(href_list.Find("action")) {action_name = lowertext(href_list["action"])} else action_name = href
	for(var/i in typesof(/topic_action) - /topic_action) if("[i]" == "/topic_action/[action_name]") {action = new i;break}
	if(!action) return null
	if(href_list.Find("arg")) {argument = href_list["arg"]}
	else {var/import = world.Import();if(import) argument = new import;else argument = null}
	return action.action(argument)

topic_action
	logins/action()
		var/retn = 0
		for(var/client/C in world) retn ++
		return retn
	who/action()
		var/list/clients = list()
		for(var/client/C in world) clients.Add(C)
		return clients
*/

client/Topic(href, list/href_list, hsrc)
	//handle other cases
	var {Page/action;action_name}
	if(href_list.Find("action")) {action_name = href_list["action"]} else action_name = href
	for(var/i in typesof(/Page) - /Page) if("[i]" == "/Page/[action_name]") {action = new i;break}
	if(!action) return null
	return action.ProcessPage(href, href_list)